﻿using DongFangGuoKai.WCSPlatform.WebApi.Extensions;
using DongFangGuoKai.WCSPlatform.WebApi.ExternalSystem.AGVCommon;
using DongFangGuoKai.WCSPlatform.WebApi.Utils;
using DongFangGuoKai.WMSPlatform.Commn.ApiResultModule;
using DongFangGuoKai.WMSPlatform.Commn.HttpModule;
using Microsoft.AspNetCore.Mvc;
using S7.Net;

namespace DongFangGuoKai.WCSPlatform.WebApi.Controllers.测试
{
    /// <summary>
    /// 提供给WMS的接口 (弃用)
    /// </summary>
    [Route("api/[controller]/[action]"), ApiController, Obsolete, ApiExplorerSettings(GroupName = nameof(WCSApiVersions.V1))]
    public class SupplyWMSController : ControllerBase
    {
        private readonly Plc plc;
        private readonly ICtmHttpClient httpClient;
        private readonly CancellationTokenSource cancellationTokenSource;
        public SupplyWMSController(Plc plc, ICtmHttpClient httpClient)
        {
            this.plc = plc;
            this.httpClient = httpClient;
            cancellationTokenSource = new CancellationTokenSource();
        }

        private static bool stopLoop = false;

        [HttpPost]
        public ActionResult<WCSApiResponse> StartGetInWhouseNum()
        {
            try
            {
                stopLoop = false;

                Task.Run(async () =>
                {
                    while (!stopLoop)
                    {
                        int? boxNumber = Convert.ToInt16(plc.Read("DB1.DBD6")); //入库箱子号
                        if (!boxNumber.HasValue || boxNumber == 0) continue;
                        var response = httpClient.Get<int>($"http://localhost:5165/api/TakeNumber/GetNumber?boxNumber={boxNumber}");

                        //三秒钟一次检测看看有没有箱子
                        await Task.Delay(TimeSpan.FromSeconds(3));
                    }
                });

                return this.Success("已开始");
            }
            catch (Exception ex)
            {
                return this.Fail(ex.Message);
            }
        }

        [HttpPost]
        public ActionResult<WCSApiResponse> StopGetInWhouseNum()
        {
            try
            {
                stopLoop = true;
                return this.Success("已停止");
            }
            catch (Exception ex)
            {
                return this.Fail(ex.Message);
            }
        }

        [HttpPost]
        public ActionResult<WCSApiResponse> GetBoxNumber()
        {
            try
            {
                return this.Success(httpClient.Get<ApiDataResult<string>>("http://10.10.1.38:8199/api/TakeNumber/GetNumber?boxNumber=1"));
            }
            catch (Exception)
            {

                throw;
            }
        }

        [HttpGet]
        public ActionResult<WCSApiResponse> AGVTest()
        {
            try
            {
                var test = new BoxApplyPassReq()
                {
                    TaskCode = "2222",
                    ReqCode = "2222",
                    Type = "1",
                };
                return this.Success(httpClient.Post<AGVBaseResModel>("http://10.10.20.180:8181/rcms/services/rest/hikRpcService/boxApplyPass",test));
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
